package org.enlongWu.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.enlongWu.mapper.ColdStorageMapper;
import org.enlongWu.mapper.MessageMapper;
import org.enlongWu.pojo.*;
import org.enlongWu.service.ColdStorageService;
import org.enlongWu.service.MessageService;
import org.enlongWu.service.SensorService;
import org.enlongWu.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Service
public class ColeStorageServiceImpl implements ColdStorageService {

    @Autowired
    private SensorService sensorService;

    @Autowired
    private ColdStorageMapper mapper;

    @Override
    public int create(ColdStorage coldStorage) {
        return mapper.insert(coldStorage);
    }

    @Override
    public ColdStorage getById(Integer id) {
        ColdStorage cs = mapper.selectById(id);
        cs.setSensors(sensorService.listByCSId(cs.getId()));
        return cs;
    }

    @Override
    public PageBean<ColdStorage> list(Integer pageNum, Integer pageSize, String type, String state, String searchString) {
        //1.创建PageBean对象
        PageBean<ColdStorage> pb = new PageBean<>();

        //2.开启分页查询 PageHelper
        PageHelper.startPage(pageNum,pageSize);

        //3.调用mapper
        List<ColdStorage> as = mapper.list(type,state,searchString);//查询到的列表
        for (int i = 0; i < as.size(); i++){//冷库中的每一个数据都为其查出所绑定的传感器列表
            as.get(i).setSensors(sensorService.listByCSId(as.get(i).getId()));
        }
        //Page中提供了方法,可以获取PageHelper分页查询后 得到的总记录条数和当前页数据
        Page<ColdStorage> p = (Page<ColdStorage>) as;

        //把数据填充到PageBean对象中
        pb.setTotal(p.getTotal());
        pb.setItems(p.getResult());
        return pb;

    }

    @Override
    public Result recreate(Integer id) {
        ColdStorage coldStorage = mapper.selectById(id);
        if (coldStorage != null){
            int j = sensorService.unbindByCSId(id);
            System.out.println("解绑"+j+"个传感器");
            //name、id、createTime不变
            coldStorage.setTemperature(null);
            coldStorage.setHumidity(null);

            coldStorage.setShelves(0);
            coldStorage.setOccupied(0);
            coldStorage.setUnitPrice((float) 0);

            coldStorage.setType(null);


            coldStorage.setTemMaximal(null);
            coldStorage.setEngineerId(null);
            coldStorage.setType(null);
            coldStorage.setAddress(null);

            coldStorage.setState("creating");
            coldStorage.setEngineerId(null);
            coldStorage.setAddress(null);
            int i = mapper.update(coldStorage);
            return Result.success(null,"成功重建并解绑"+j+"个传感器");
        }else return Result.error("重建失败");

    }

    @Override
    public int init(Integer id,String type) {
        ColdStorage coldStorage = mapper.selectById(id);
        coldStorage.setState("idling");
        coldStorage.setType(type);
        int i = mapper.update(coldStorage);
        return i;
    }

    @Override
    public int run(Integer id, Float unitPrice, Float temMaximal, Float temMinimal, Float humMaximal, Float humMinimal) {
        ColdStorage coldStorage = mapper.selectById(id);
        if (unitPrice != null){
            coldStorage.setUnitPrice(unitPrice);
        }
        coldStorage.setTemMaximal(temMaximal);
        coldStorage.setTemMinimal(temMinimal);
        coldStorage.setHumMaximal(humMaximal);
        coldStorage.setHumMinimal(humMinimal);
        coldStorage.setState("running");
        int i = mapper.update(coldStorage);
        return i;
    }

    /**
     * 终止
     * @param id
     * @return 0(还有货物，不准终止，请先清空货物)；1成功终止
     */
    @Override
    public int stop(Integer id) {
        ColdStorage coldStorage = mapper.selectById(id);
        if (coldStorage.getOccupied() != null && coldStorage.getOccupied() != 0){
            return 0;
        }
        coldStorage.setState("idling");
        int i = mapper.update(coldStorage);
        return i;
    }


//    /**
//     * 当前用户信箱
//     * 首先创建一个PageBean对象，然后使用PageHelper插件进行分页查询。
//     * 接着调用mapper接口的list方法获取列表，并将结果封装到PageBean对象中返回。
//     *
//     * @param pageNum     当前页码
//     * @param pageSize    每页显示的记录数
//     * @param messageType 分类ID
//     * @param state       文章状态
//     * @return
//     */
//    @Override
//    public PageBean<Message> list(Integer pageNum, Integer pageSize, Integer messageType, String state) {
//        //1.创建PageBean对象
//        PageBean<Message> pb = new PageBean<>();
//
//        //2.开启分页查询 PageHelper
//        PageHelper.startPage(pageNum, pageSize);
//
//        //3.调用mapper
//        Map<String, Object> map = ThreadLocalUtil.get();
//        Integer userId = (Integer) map.get("id");
//        List<Message> as = messageMapper.list(userId, messageType, state);//查询到的文章
//        //Page中提供了方法,可以获取PageHelper分页查询后 得到的总记录条数和当前页数据
//        Page<Message> p = (Page<Message>) as;
//
//        //把数据填充到PageBean对象中
//        pb.setTotal(p.getTotal());
//        pb.setItems(p.getResult());
//        return pb;
//    }
//
//    @Override
//    public Result reading(Integer messageid) {
//        Message message = messageMapper.searchById(messageid);
//        if (message == null) {
//            return Result.error("查无此号");//查无此号
//        } else if (message.getState().equals("已读")) {
//            return Result.error("本就已读");//本就已读
//        } else {
//            messageMapper.setRead(messageid);
//            return Result.success("文章[" + message.getTitle() + "]标记成功");//操作成功
//        }
//    }
//
//    @Override
//    public List<Message> applicationGroupBySendTime(Integer senderId) {
//        return messageMapper.applicationGroupBySendTime(senderId);
//    }
//
//    @Override
//    public Message findById(Integer messageId) {
//        return messageMapper.searchById(messageId);
//    }
//
//    @Override
//    public Result delete(Integer messageId) {
//        int i = messageMapper.delete(messageId);
//        if (i == 1) {
//            return Result.success(null,"删除了"+i+"条消息");
//        }else {
//            return Result.error("删除失败");
//        }
//    }
//
//    @Override
//    public int deleteByContent(Integer applicationId) {
//        return messageMapper.deleteByContent(applicationId.toString());
//    }


}
